package com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.customview.MyRecyclerView;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices.FirstChangeDevicesActivity;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.adpater.MyLeftAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.adpater.MyMainDevicesHotAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.persenter.FirstChangeMainPersenter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.view.ISFirstChangeMainView;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.FirstChangeSettingActivity;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeSearch.FirstChangeSearchActivity;
import com.example.anew.journey.mynewjourney.firstchangelogic.personalInformation.FirstChangePersonalInformationActivity;
import com.example.anew.journey.mynewjourney.tool.GlideCircleTransform;
import com.example.anew.journey.mynewjourney.utils.AntiShake;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/13
 */

public class FirstChangeMainActivity extends AppCompatActivity implements ISFirstChangeMainView {
    @Bind(R.id.activity_first_change_main_home_page_tv_my_devics)
    TextView activityFirstChangeMainHomePageTvMyDevics;
    @Bind(R.id.activity_first_change_main_home_page_rl_my_devics)
    RelativeLayout activityFirstChangeMainHomePageRlMyDevics;
    @Bind(R.id.activity_first_change_main_home_page_iv_left_linkage)
    ImageView activityFirstChangeMainHomePageIvLeftLinkage;
    @Bind(R.id.activity_first_change_main_home_page_tv_titile_name)
    TextView activityFirstChangeMainHomePageTvTitileName;
    @Bind(R.id.activity_first_change_main_home_page_iv_search_bt)
    ImageView activityFirstChangeMainHomePageIvSearchBt;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.activity_first_change_main_dy_lateral_spreads_menu)
    DrawerLayout activityFirstChangeMainDyLateralSpreadsMenu;
    @Bind(R.id.activity_first_change_main_left_seting)
    ConstraintLayout activityFirstChangeMainDyLateSeting;
    @Bind(R.id.app_bar)
    AppBarLayout appBar;
    @Bind(R.id.activity_first_change_main_home_page_my_recyclerview)
    MyRecyclerView activityFirstChangeMainHomePageMyRecyclerview;
    @Bind(R.id.activity_first_change_main_home_page_fb_add)
    FloatingActionButton activityFirstChangeMainHomePageFbAdd;
    @Bind(R.id.no_date_picture)
    ImageView noDatePicture;
    @Bind(R.id.activity_first_change_main_left_popup_iv_head_portrait)
    ImageView activityFirstChangeMainLeftPopupIvHeadPortrait;
    @Bind(R.id.activity_first_change_main_left_popup_tv_name)
    TextView activityFirstChangeMainLeftPopupTvName;
    @Bind(R.id.activity_first_change_main_left_popup_iv_next_step)
    ImageView activityFirstChangeMainLeftPopupIvNextStep;
    @Bind(R.id.activity_first_change_main_left_popup_rl_head)
    RelativeLayout activityFirstChangeMainLeftPopupRlHead;
    @Bind(R.id.activity_first_change_main_left_popup_left_recyclerview)
    RecyclerView activityFirstChangeMainLeftPopupLeftRecyclerview;
    private MyLeftAdapter adapter;
    private MyMainDevicesHotAdapter myMainDevicesHotAdapter;
    public AntiShake util;
    private FirstChangeMainPersenter mFirstChangeMainPersenter = new FirstChangeMainPersenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_change_main);
        ButterKnife.bind(this);
        // 防抖动工具类
        util = new AntiShake();
        init();
        initTitle();
    }

    private void initTitle() {
        setSupportActionBar(toolbar);
        activityFirstChangeMainHomePageFbAdd.post(new Runnable() {
            @Override
            public void run() {
                mFirstChangeMainPersenter.guidePager(activityFirstChangeMainHomePageFbAdd,FirstChangeMainActivity.this);
            }
        });
        mFirstChangeMainPersenter.initializeSidebar(this);
        mFirstChangeMainPersenter.HomepageEquipmentList(activityFirstChangeMainHomePageMyRecyclerview,this);
    }
    private void init() {
        mFirstChangeMainPersenter.SidebarOptions(activityFirstChangeMainLeftPopupLeftRecyclerview,FirstChangeMainActivity.this);
    }


    @OnClick({R.id.activity_first_change_main_home_page_iv_left_linkage, R.id.activity_first_change_main_home_page_iv_search_bt, R.id.activity_first_change_main_home_page_fb_add, R.id.activity_first_change_main_left_popup_rl_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_first_change_main_home_page_iv_left_linkage:
                activityFirstChangeMainDyLateralSpreadsMenu.openDrawer(activityFirstChangeMainDyLateSeting);
                break;
            case R.id.activity_first_change_main_home_page_iv_search_bt:
//                if (util.check()) return;
//                mFirstChangeMainPersenter.onClickSearch();
                Toast.makeText(this,"敬请期待",Toast.LENGTH_SHORT).show();
//                Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeHistoricalRecordCardiacDetectionActvity.class); //FirstChangeDevicesActivity
//                intent.putExtra("my_test_date",120+"");
//                intent.putExtra("my_test_desc","你的宝宝健康发育，心率值正常，祝你生活愉快！");
//                startActivity(intent);
                break;
            case R.id.activity_first_change_main_home_page_fb_add:
               // if (util.check()) return;
                mFirstChangeMainPersenter.onClickFAB();
                break;
            case R.id.activity_first_change_main_left_popup_rl_head:
                //if (util.check()) return;
                mFirstChangeMainPersenter.onClickPersonalInformationPage();
                break;
        }
    }


    // TODO 获取焦点
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        mFirstChangeMainPersenter.startViewSidebar(this);
        mFirstChangeMainPersenter.loadMyDevices(this);

    }
    // TODO 失去焦点  可以进行一些轻量级的存储数据和去初始化工作
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private void selectorOptions(int postion) {
    }

    // 获取头像
    @Override
    public String getHeadPortrait() {
        return null;
    }
    // 设置头像
    @Override
    public void setHeadPortrait(String str) {
        String URL = RBConstants.SERVER_URL +"/Public/Uploads/"+str;
        String URL1 = RBConstants.SERVER_URL +str;
            Glide
                .with(this)
                .load(URL)
                .transform(new BitmapTransformation[]{new GlideCircleTransform(this)})
                .placeholder(R.drawable.photo).error(R.drawable.photo)
                .into(activityFirstChangeMainLeftPopupIvHeadPortrait);
    }
    // 获取名字
    @Override
    public String getName() {
        return activityFirstChangeMainLeftPopupTvName.getText().toString().trim();
    }
    //设置名字
    @Override
    public void setName(String str) {
        activityFirstChangeMainLeftPopupTvName.setText(str);
    }
    // 跳转到设备页面
    @Override
    public void toDevicesActivity() {
        Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeDevicesActivity.class); //FirstChangeDevicesActivity
        intent.putExtra("device_page_identification",0);
        startActivity(intent);
    }
    // 跳转到设置页面
    @Override
    public void toSettingActivity() {
        Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeSettingActivity.class);
        startActivity(intent);
    }
    // 侧边栏头部背景
    @Override
    public void setSideHeadBackground() {

    }
    // 跳转到搜索页面
    @Override
    public void toSearchActivity() {
        Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeSearchActivity.class);
        startActivity(intent);
    }
    // todo 跳转到添加设备页面
    @Override
    public void toAddDevicsActivity() {
//       Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeAddDevicesActvity.class);
//        startActivity(intent);

        Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangeDevicesActivity.class);
        intent.putExtra("device_page_identification",1);
        startActivity(intent);
    }
    // 跳转到个人信息页面
    @Override
    public void toPersonalInformationPage() {
        Intent intent =  new Intent(FirstChangeMainActivity.this, FirstChangePersonalInformationActivity.class);
        startActivity(intent);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            return true;
//        }
//        return false;
//    }
}
